﻿using System.Threading;
using System.Web.Mvc;
using DevFramework.Core.Aspects.ValidationAspects;
using DevFramework.Core.CrossCuttingConcern.ExceptionHandling.Exceptions;
using DevFramework.Business.Abstract;
using DevFramework.Business.Concrete.Managers;
using DevFramework.Business.ValidationRules.FluentValidation;
using DevFramework.MvcUI.Filters;
using DevFramework.MvcUI.Infrastructure;
using DevFramework.Entities.ComplexType;

namespace DevFramework.MvcUI.Controllers
{
    
    public class CartController : BaseController
    {

        private readonly IProductService _productService;

        public CartController(IProductService productService)
        {
            _productService = productService;
        }

        public ActionResult AddToCart(CartManager sepet, int productId)
        {
            var urun = _productService.Get(productId);

            var cart = new Cart
            {
                Product = urun,
                Quantity = 1,
            };

            sepet.Ekle(cart);

            return PartialView("CartSummary", sepet);
        }

        public ActionResult RemoveFromCart(CartManager sepet, int productId)
        {
            sepet.Remove(productId);

            return PartialView("CartSummary", sepet);
        }

        public ActionResult Index(CartManager sepet)
        {
            return View(sepet);
        }

        public PartialViewResult CartSummary(CartManager sepet)
        {
            return PartialView(sepet);
        }

        public ActionResult Checkout()
        {
            return View(new ShippingDetail());
        }

        [HttpPost]
        [ExceptionFilter]
        public ActionResult Checkout(CartManager cartManager, ShippingDetail shippingDetails)
        {
            SuccessNotification("Siparişiniz Alınmıştır. Bizi seçtiğiniz için teşekkür ederiz.");
            Session["Cart"] = null;
            return RedirectToAction("Index", "Product");
            //return View("Completed", shippingDetails);
        }
    }
}